//
//  FQDLoginViewController.m
//  FakeQDaily
//
//  Created by mewxiv on 9/6/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDLoginViewController.h"
#import "FQDAccountManager.h"
#import "QDailyAPI.h"
#import "FQDAccountInputField.h"
#import "FQDNavigationView.h"
#import "NSString+FQDValidation.h"

#import "Masonry.h"

@interface FQDLoginViewController () <UITextFieldDelegate>

// QIcon图标
@property (nonatomic, strong) UIImageView *qIconImageView;
// 手机号码或邮箱
@property (nonatomic, strong) FQDAccountInputField *phoneOrMailInputField;
// 密码
@property (nonatomic, strong) FQDAccountInputField *passwordInputField;
// 忘记密码按钮
@property (nonatomic, strong) UIButton *forgetPassButton;
// 登录按钮
@property (nonatomic, strong) UIButton *loginButton;
// 注册新账号按钮
@property (nonatomic, strong) UIButton *registerNewButton;

@end

@implementation FQDLoginViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.hideKeyboardOnTap = YES;
    [self setupTopUI];
    [self setupBottomUI];
}

- (void)setupTopUI
{
    self.navView.titleLabel.text = @"登录";
    
    UIImage *iconImage = [UIImage imageNamed:@"c_Qdaily button"];
    self.qIconImageView = [[UIImageView alloc] init];
    self.qIconImageView.image = iconImage;
    [self.view addSubview:self.qIconImageView];
    
    WeakSelf;
    
    [self.qIconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.view).offset(90);
        make.centerX.equalTo(weakSelf.view);
        make.size.equalTo(@80);
    }];
}

- (void)setupBottomUI
{
    self.phoneOrMailInputField = [[FQDAccountInputField alloc] init];
    [self.phoneOrMailInputField setPlaceHolderString:@"手机号码/邮箱"];
    [self.view addSubview:self.phoneOrMailInputField];
    
    UITextField *phoneOrMailTextField = self.phoneOrMailInputField.textField;
    phoneOrMailTextField.keyboardType = UIKeyboardTypeDefault;
    phoneOrMailTextField.returnKeyType = UIReturnKeyDone;
    phoneOrMailTextField.delegate = self;
    
    self.passwordInputField = [[FQDAccountInputField alloc] init];
    [self.passwordInputField setPlaceHolderString:@"密码"];
    [self.view addSubview:self.passwordInputField];
    
    UITextField *passwordTextField = self.passwordInputField.textField;
    passwordTextField.secureTextEntry = YES;
    passwordTextField.keyboardType = UIKeyboardTypeDefault;
    passwordTextField.returnKeyType = UIReturnKeyDone;
    passwordTextField.delegate = self;
    
    WeakSelf;
    [self.phoneOrMailInputField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.qIconImageView.mas_bottom).offset(37);
        make.left.and.right.equalTo(weakSelf.view);
        make.height.equalTo(@44);
    }];
    
    [self.passwordInputField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.phoneOrMailInputField.mas_bottom);
        make.left.and.right.equalTo(weakSelf.view);
        make.height.equalTo(@44);
    }];
    
    self.forgetPassButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.forgetPassButton addTarget:self action:@selector(forgetPassword:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.forgetPassButton];
    
    [self.forgetPassButton setTitle:@"忘记密码" forState:UIControlStateNormal];
    [self.forgetPassButton setTitleColor:[UIColor colorWithWhite:0.6f alpha:1.0f] forState:UIControlStateNormal];
    self.forgetPassButton.titleLabel.font = FQDRegularFontWithSize(14.0f);
    
    
    self.loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.loginButton setImage:[UIImage imageNamed:@"Next Button"] forState:UIControlStateNormal];
    [self.loginButton addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.loginButton];
    
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.passwordInputField.mas_bottom).offset(60);
        make.centerX.equalTo(weakSelf.view);
        make.size.mas_equalTo(CGSizeMake(163, 44));
    }];
    
    self.registerNewButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.registerNewButton addTarget:self action:@selector(registerNew:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.registerNewButton];
    
    [self.registerNewButton setTitle:@"注册新账号" forState:UIControlStateNormal];
    [self.registerNewButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.registerNewButton.titleLabel.font = FQDRegularFontWithSize(14.0f);
    
    [self.registerNewButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf.view);
        make.bottom.equalTo(weakSelf.view).offset(-15);
        make.size.mas_equalTo(CGSizeMake(100, 50));
    }];
}

#pragma mark - Event Response

- (void)forgetPassword:(id)sender
{
    
}

- (void)registerNew:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)login:(id)sender
{
    NSString *phoneOrEmail = self.phoneOrMailInputField.textField.text;
    NSString *password = self.passwordInputField.textField.text;

    if ([phoneOrEmail length] > 0 && [password length] > 0) {
        if ([phoneOrEmail isValidateMobile]) {
            [[FQDAccountManager sharedManager] loginWithPhone:phoneOrEmail password:password remember:YES
             completion:^(BOOL success) {
                if (success) {
                    [self dismissViewControllerAnimated:YES completion:nil];
                }
            }];
        } else {
            [[FQDAccountManager sharedManager] loginWithEmail:phoneOrEmail password:password remember:YES
             completion:^(BOOL success) {
                if (success) {
                    [self dismissViewControllerAnimated:YES completion:nil];
                }
            }];
        }
    }
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

@end
